package cn.zyx.pet.service.impl;

import cn.zyx.basic.service.impl.BaseServiceImpl;
import cn.zyx.basic.util.PageList;
import cn.zyx.pet.domain.Pet;
import cn.zyx.pet.domain.PetDetail;
import cn.zyx.pet.mapper.PetDetailMapper;
import cn.zyx.pet.mapper.PetMapper;
import cn.zyx.pet.query.PetQuery;
import cn.zyx.pet.service.IPetService;
import cn.zyx.user.domain.User;
import cn.zyx.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {


    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(Pet pet) {
        // 保存基本服务信息
        petMapper.save(pet);
        // 保存服务详情信息
        PetDetail detail = pet.getDetail();
        detail.setPet_id(pet.getId());
        petDetailMapper.save(detail);
    }

    @Override
    public void update(Pet pet) {
        PetDetail detail = pet.getDetail();
        if (detail != null){
            detail.setPet_id(pet.getId());
            if (detail.getId() != null){
                petDetailMapper.update(detail);
            }else {
                petDetailMapper.save(detail);
            }
        }
        petMapper.update(pet);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 删除服务详情
        petDetailMapper.removeByPetId(id);
        // 删除服务
        petMapper.remove(id);
    }

    @Override
    public PetDetail getDetailByPetId(Long petId) {
        return petDetailMapper.loadDetailByPetId(petId);
    }

    @Override
    public void onOrOffSale(List<Long> ids, int flag) {
        // 判断是上架还是下架 -- 上架是1 下架是0
        Map<String,Object> map = new HashMap<>();   // 用map相当于我们mapper接口多个参数用@param注解，sql使用key取值
        map.put("ids", ids);
        if (flag == 1){
            // 调整上架时间
            map.put("onsaleTime", new Date());
            petMapper.onSale(map);
        }else {
            // 调整下架时间
            map.put("offsaleTime", new Date());
            petMapper.offSale(map);
        }
    }

    @Override
    @Transactional
    public void adopt(Long petId, Long loginInfoId) {
        // 修改状态 -- 把状态改成下架
        Pet pet = petMapper.loadById(petId);
        pet.setState(0);  // 下架
        pet.setOffsaletime(new Date()); // 生成下架时间
        // 绑定领养的用户
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        pet.setUser(user);
        pet.setUser_id(user.getId());
        // 修改数据库数据
        petMapper.update(pet);
        // 生成订单 @todo 生成领养订单和支付
    }

    @Override
    public PageList<Pet> adoptPet(PetQuery petQuery, Long loginInfoId) {
        // 通过当前登录人的信息去数据库查询当前用户
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        petQuery.setUser_id(user.getId());
        // 查询总条数
        Integer total = petMapper.queryCount(petQuery);
        // 查询分页数据
        List<Pet> rows = petMapper.queryData(petQuery);
        return new PageList<>(total,rows);
    }

}
